package server

import (
	"context"
	"github.com/go-kratos/kratos/v2/middleware/selector"
	"github.com/google/wire"
)

// ProviderSet is server providers.
var ProviderSet = wire.NewSet(NewGRPCServer, NewHTTPServer)

var whiteList = map[string]bool{
	"/api.bff.v1.User/Messages": true,

	"/api.bff.v1.Blogs/GetBlogs":                true,
	"/api.bff.v1.Blogs/ListBlogs":               true,
	"/api.bff.v1.Blogs/HotBlogs":                true,
	"/api.bff.v1.Blogs/ListBlogTags":            true,
	"/api.bff.v1.BlogsComment/ListBlogsComment": true,
	"/api.bff.v1.BlogsComment/GetBlogsComment":  true,
	"/api.bff.v1.Blogs/ListGraph":               true,

	"/api.bff.v1.Account/CreateAccount":  true,
	"/api.bff.v1.Account/ResetPassword":  true,
	"/api.bff.v1.Account/LoginByAccount": true,

	"/api.bff.v1.Tools/UploadFile": true,
	"/api.bff.v1.Tools/Files":      true,

	"/api.bff.v1.Travels/GetTravel":  true,
	"/api.bff.v1.Travels/ListTravel": true,
	"/api.bff.v1.Travels/ListTags":   true,
}

func NewWhiteListMatcher() selector.MatchFunc {
	return func(ctx context.Context, operation string) bool {
		return whiteList[operation]
	}
}

var tempAuthOperations = map[string]bool{
	"/api.bff.v1.User/GetUser":        true,
	"/api.bff.v1.User/ListProject":    true,
	"/api.bff.v1.User/GetProject":     true,
	"/api.bff.v1.User/ListExperience": true,
	"/api.bff.v1.User/GetExperience":  true,
}

// 临时身份认证匹配器
func NewTempAuthMatcher() selector.MatchFunc {
	return func(ctx context.Context, operation string) bool {
		// 指定需要临时身份认证的接口
		return tempAuthOperations[operation]
	}
}

// 常规身份认证匹配器
func NewRegularAuthMatcher() selector.MatchFunc {
	return func(ctx context.Context, operation string) bool {
		// 排除白名单和临时认证的接口，剩下的走常规认证
		whiteOk := NewWhiteListMatcher()(ctx, operation)
		tempOk := NewTempAuthMatcher()(ctx, operation)
		return !whiteOk && !tempOk
	}
}
